package com.joysuch.wwyt.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Splitter;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.risk.entity.RiskControllevels;
import com.joysuch.wwyt.risk.entity.RiskLevelDefaultControlLevel;
import com.joysuch.wwyt.risk.entity.RiskPointEvaluationDetail;
import com.joysuch.wwyt.risk.entity.dto.RiskControllevelsDto;
import com.joysuch.wwyt.risk.mapper.RiskControllevelsMapper;
import com.joysuch.wwyt.risk.repository.RiskPointEvaluationDetailDao;
import com.joysuch.wwyt.risk.service.RiskControlLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.risk.service.RiskLevelDefaultControlLevelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author heqifeng
 * @since 2021-08-31
 */
@Service
public class RiskControllevelsServiceImpl extends ServiceImpl<RiskControllevelsMapper, RiskControllevels> implements RiskControlLevelService {

    @Autowired
    RiskControllevelsMapper riskControllevelsMapper;
    @Autowired
    RiskPointEvaluationDetailDao riskPointEvaluationDetailDao;
    @Autowired
    RiskLevelDefaultControlLevelService riskLevelDefaultControlLevelService;


    @Override
    public List<RiskControllevels> getAllLevels() {
        Long tnenetId = Context.getCurrentTenantId();
        String orgCode = Context.getCompanyCode();
        LambdaQueryWrapper<RiskControllevels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RiskControllevels::getDeleteFlag,DeleteFlag.EFFECTIVE.getCode());
        wrapper.likeRight(RiskControllevels::getOrgCode,orgCode);
        wrapper.eq(RiskControllevels::getTenentId,tnenetId);
        return riskControllevelsMapper.selectList(wrapper);
    }

    @Override
    public RiskControllevels getById(Long id) {
        LambdaQueryWrapper<RiskControllevels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RiskControllevels::getId, id);
        wrapper.eq(RiskControllevels::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
        return riskControllevelsMapper.selectOne(wrapper);
    }

    @Override
    public RiskControllevels getByCode(Long code) {
        LambdaQueryWrapper<RiskControllevels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RiskControllevels::getCode, code);
        wrapper.eq(RiskControllevels::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
        wrapper.like(RiskControllevels::getOrgCode, Context.getCurrentOrgCode() + Constant.SQL_LIKE);
        wrapper.eq(RiskControllevels::getTenentId, Context.getCurrentTenantId());
        return riskControllevelsMapper.selectOne(wrapper);
    }

    @Override
    public int deleteById(Long id) {
        RiskControllevels level = riskControllevelsMapper.selectById(id);
        // 校验当前管控层级是否已绑定管控措施
        // wwyt_risk_point_business_evaluation_detail中的control_level_id字段实际存的是controlLevel的code。
        List<RiskPointEvaluationDetail> details = riskPointEvaluationDetailDao.findByControlLevelId(level.getCode());
        if (details.size() > 0){
            return -1;
        }
        // 校验当前管控层级是否是某个危险等级的默认管控层级
        List<RiskLevelDefaultControlLevel> levelDefaultControlLevels = riskLevelDefaultControlLevelService.selectByControlLevelCode(level.getCode().toString());
        if (levelDefaultControlLevels.size() > 0){
            return -1;
        }
        level.setDeleteFlag(DeleteFlag.DELETED.getCode());
        return riskControllevelsMapper.updateById(level);
    }

    @Override
    public boolean insertOrUpdate(RiskControllevels riskControllevels) {
        // 检查code或name是否被占用
        if (riskControllevels.getCode() != null) {
            LambdaQueryWrapper<RiskControllevels> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RiskControllevels::getCode, riskControllevels.getCode());
            wrapper.eq(RiskControllevels::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
            wrapper.likeRight(RiskControllevels::getOrgCode, Context.getCompanyCode());
            wrapper.likeRight(RiskControllevels::getTenentId, Context.getCurrentTenantId());
            // id存在时将执行更新，数据库中与riskControllevels id相同的数据不参与判重
            if (riskControllevels.getId() != null){
                wrapper.ne(RiskControllevels::getId,riskControllevels.getId());
            }
            Integer codeCount = riskControllevelsMapper.selectCount(wrapper);
            Assert.isTrue(codeCount < 1, String.format(Status.CODE_WAS_EXIST.getMsg(), riskControllevels.getCode()));
        }
        if (riskControllevels.getName() != null) {
            LambdaQueryWrapper<RiskControllevels> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RiskControllevels::getName, riskControllevels.getName());
            wrapper.likeRight(RiskControllevels::getOrgCode, Context.getCompanyCode());
            wrapper.likeRight(RiskControllevels::getTenentId, Context.getCurrentTenantId());
            wrapper.eq(RiskControllevels::getDeleteFlag, DeleteFlag.EFFECTIVE.getCode());
            // id存在时将执行更新，数据库中与riskControllevels id相同的数据不参与判重
            if (riskControllevels.getId() != null){
                wrapper.ne(RiskControllevels::getId,riskControllevels.getId());
            }
            Integer nameCount = riskControllevelsMapper.selectCount(wrapper);
            Assert.isTrue(nameCount < 1, String.format(Status.NAME_WAS_EXIST.getMsg(), riskControllevels.getName()));
        }
        // 新增时code自增
        if (riskControllevels.getId() == null && riskControllevels.getCode() == null){
            Long maxCode = riskControllevelsMapper.getMaxCode(Context.getCurrentTenantId(), Context.getCurrentOrgCode());
            riskControllevels.setCode(maxCode != null ? ++maxCode : 0);
        }
        return saveOrUpdate(riskControllevels);
    }

    @Override
    public List<SimpleSelectListBean> getAllForSimpleSelect() {
        List<RiskControllevels> levels = getAllLevels();
        List<SimpleSelectListBean> simpleSelectList = new ArrayList<>();
        for (RiskControllevels level : levels) {
            simpleSelectList.add(converToSimpleSelectListBean(level));
        }
        return simpleSelectList;
    }

    public SimpleSelectListBean converToSimpleSelectListBean(RiskControllevels levels){
        SimpleSelectListBean bean = new SimpleSelectListBean();
        bean.setValue(levels.getCode());
        bean.setLabel(levels.getName());
        return bean;
    }

    public List<RiskControllevelsDto> convertToDto(List<RiskControllevels> levels){
        List<RiskControllevelsDto> dtos = new ArrayList<>();
        for (RiskControllevels level : levels) {
            RiskControllevelsDto dto = new RiskControllevelsDto();
            dto.setId(level.getId());
            dto.setCode(level.getCode());
            dto.setName(level.getName());
            dtos.add(dto);
        }
        return dtos;
    }
}
